//
//  JJCitiesTableViewController.m
//  Group4KY
//
//  Created by 一介布衣 on 16/4/6.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

#import "JJCitiesTableViewController.h"
#import <AFNetworking.h>
#import "CityModel.h"
static NSString *citiesCellId = @"citiesCellId";

@interface JJCitiesTableViewController ()


@end

@implementation JJCitiesTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"城市";
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:citiesCellId];
    
    [self getCitiesData];
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)back:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)getCitiesData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //        http://120.77.213.62:9080
    NSString *url =ServiceUrl@"/areaProvinceCityApp/searchCity";
    NSDictionary *parameter = @{@"provinceCode":self.provinceCode};
    [manager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dict in array) {
            CityModel *model = [[CityModel alloc] initWithDictionry:dict];
            NSLog(@"%@",model.city);
            [self.citiesArr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.citiesArr.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:citiesCellId forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    CityModel *model = self.citiesArr[indexPath.row];
    cell.textLabel.text = model.city;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityModel *model = self.citiesArr[indexPath.row];
    NSString *str = model.city;
    NSString *str1 = model.cityCode;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"cityName" object:self userInfo:@{@"cityName":str,@"cityCode":str1}];
    NSLog(@"%@",str1);
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return 1.0f;
    return 44.0f;
}
-(NSMutableArray *)citiesArr{
    if (_citiesArr == nil) {
        _citiesArr = [[NSMutableArray alloc] init];
    }
    return _citiesArr;
}
@end









